(function() {
    angular
        .module('ydApp')
        .directive('navButton', function() {
            return {
                restrict: "EA",
                template: '<button class="button" ng-transclude></button>',
                replace: true,
                transclude: true,
                controller: function($scope, $location, $ionicHistory,$state) {
                    $scope.link = function(url) {
                        $location.url(url);
                    }
                    $scope.back = function(url) {
                        if ($scope.confirm) {
                            var result = $scope.confirm();
                            if (result != null) {
                                console.log(result)
                                result.then(function(res) {
                                    if (res) {
                                        $ionicHistory.goBack();
                                    }
                                })
                            } else {
                                $ionicHistory.goBack();
                            }
                        } else {
                            if (url) {
                                $state.go(url);
                            }
                            else {
                                $ionicHistory.goBack();
                            }
                        }

                    }
                }
            };
        })
})();
